<template>
  <div v-loading.lock="loading" class="base-content">
    <div class="basic-info">
      <el-form
        ref="queryForm"
        :inline="true"
        :model="queryForm"
        :label-width="$i18n.locale === 'en' ? '160px' : '100px'"
      >
        <el-form-item :label="dataList.DeviceBorrowNumber" prop="num">
          <el-input
            v-model="queryForm.num"
            :placeholder="dataList.DeviceBorrowNumber"
          />
        </el-form-item>
        <el-form-item :label="dataList.Status" prop="statusId">
          <el-select
            v-model="queryForm.statusId"
            :placeholder="dataList.Placeholder"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="dataList.DeviceNumber" prop="deviceNum">
          <el-input
            v-model="queryForm.deviceNum"
            :placeholder="dataList.DeviceNumber"
          />
        </el-form-item>
        <el-form-item :label="dataList.DeviceBorrower" prop="borrowPersonName">
          <el-input
            v-model="queryForm.borrowPersonName"
            :placeholder="dataList.DeviceBorrower"
          />
        </el-form-item>
        <el-form-item :label="dataList.CreateTime" prop="createTimeArray">
          <el-date-picker
            v-model="queryForm.createTimeArray"
            type="daterange"
            :range-separator="dataList.To"
            :start-placeholder="dataList.StartDate"
            :end-placeholder="dataList.EndDate"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>

      <div class="base-content-operate">
        <el-button type="primary" @click="onQuery">{{ btn.Query }}</el-button>
        <el-button @click="cancelForm">{{ btn.Reset }}</el-button>
        <el-button icon="el-icon-plus" type="primary" @click="onNew">{{
          btn.Add
        }}</el-button>
      </div>

      <el-table
        :data="list"
        element-loading-text="Loading"
        fit
        highlight-current-row
        :row-class-name="shouldHighlightRow"
      >
        <el-table-column
          align="center"
          :label="dataList.Number"
          :width="$i18n.locale === 'en' ? 80 : 50"
        >
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.DeviceBorrowNumber">
          <template slot-scope="scope">
            <el-link type="primary" @click="toView(scope.row)">
              {{ scope.row.num }}</el-link
            >
          </template>
        </el-table-column>
        <el-table-column :label="dataList.BorrowType">
          <template slot-scope="scope">
            {{ scope.row.typeName }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.Status">
          <template slot-scope="scope">
            <span>{{ scope.row.statusName }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="dataList.ReasonsOfBorrow">
          <template slot-scope="scope">
            {{ scope.row.reason }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.DeviceBorrowTime">
          <template slot-scope="scope">
            {{ formatTime(scope.row.borrowTime) }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.DeviceBorrowReturnTime">
          <template slot-scope="scope">
            {{ formatTime(scope.row.returnDate) }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.DeviceBorrower">
          <template slot-scope="scope">
            {{
              scope.row.type == neibu
                ? scope.row.borrowPersonName
                : scope.row.customerName
            }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.Creator">
          <template slot-scope="scope">
            {{ scope.row.creatorName }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.CreateTime">
          <template slot-scope="scope">
            {{ scope.row.createTime }}
          </template>
        </el-table-column>
        <el-table-column :label="dataList.Operation">
          <template slot-scope="scope">
            <div class="base-table-operation">
              <el-link
                v-if="scope.row.statusId == 10 || scope.row.statusId == 40"
                size="mini"
                type="primary"
                @click="handleEdit(scope.row)"
                >{{ btn.Edit }}</el-link
              >
              <el-link
                v-if="scope.row.statusId == 10 || scope.row.statusId == 40"
                size="mini"
                type="danger"
                @click="handleRepeal(scope.row)"
                >{{ btn.Nullify }}</el-link
              >
              <el-link
                v-if="scope.row.statusId == 30"
                size="mini"
                type="primary"
                @click="lendAffirm(scope.row)"
                >{{ dataList.BorrowConfilm }}</el-link
              >
              <el-link
                v-if="scope.row.statusId == 50"
                size="mini"
                type="primary"
                @click="returnDispose(scope.row)"
                >{{ dataList.ReturnProcessing }}</el-link
              >
            </div>
          </template>
        </el-table-column>
      </el-table>

      <BasePagination
        :total="pagination.total"
        :current-page="pagination.pageIndex"
        :page-count="pagination.total"
        @changePage="changePage"
        @sizeChange="sizeChange"
      />
    </div>
    <DeviceBorrowEdit
      v-if="dialogFormAddVisible"
      ref="dialogFormAddVisible"
      :dialog-form-edit-visible.sync="dialogFormAddVisible"
      edit-state.sync="false"
    />

    <DeviceBorrowEdit
      v-if="dialogFormEditVisible"
      :id.sync="id"
      ref="dialogFormEditVisible"
      :dialog-form-edit-visible.sync="dialogFormEditVisible"
      :edit-state.sync="editState"
    />

    <DeviceBorrowConfirmDialog
      v-if="lendAffirmVisible"
      :id.sync="id"
      ref="DeviceBorrowConfirmDialog"
      :dialog-form-visible.sync="lendAffirmVisible"
    />
    <DeviceBorrowSendBackDialog
      v-if="deviceBorrowSendBackVisible"
      :id.sync="id"
      ref="DeviceBorrowSendBackDialog"
      :dialog-form-visible.sync="deviceBorrowSendBackVisible"
    />
    <DeviceBorrowViewDialog
      v-if="deviceBorrowViewVisible"
      :id.sync="id"
      ref="DeviceBorrowViewDialog"
      :dialog-form-visible.sync="deviceBorrowViewVisible"
    />
  </div>
</template>

<script>
import { getList, updateStatus } from "../../api/DeviceBorrow.js";
import DeviceBorrowEdit from "./DeviceBorrowAddDialog.vue";
import DeviceBorrowConfirmDialog from "./DeviceBorrowConfirmDialog.vue";
import DeviceBorrowViewDialog from "./DeviceBorrowViewDialog.vue";
import DeviceBorrowSendBackDialog from "./DeviceBorrowSendBackDialog.vue";
import { borrowStatus, neibu } from "../../utils/const";
import { formatTime } from "../../utils/time";
import { formatTimeMin, formatTimeMax } from "../../utils/time";
export default {
  name: "DeviceBorrowList",
  components: {
    DeviceBorrowEdit,
    DeviceBorrowConfirmDialog,
    DeviceBorrowViewDialog,
    DeviceBorrowSendBackDialog,
  },
  data() {
    return {
      neibu,
      options: borrowStatus,
      id: 0,
      deviceBorrowSendBackVisible: false,
      deviceBorrowViewVisible: false,
      lendAffirmVisible: false,
      loading: true,
      editState: false,
      list: [],
      queryForm: {
        num: "",
        statusId: -1,
        deviceName: "",
        borrowPersonName: "",
        createTimeArray: [],
        departId: sessionStorage.getItem("currentDepartmentId"),
      },
      dialogFormAddVisible: false,
      dialogFormEditVisible: false,
      pagination: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
    };
  },
  computed: {
    btn() {
      return this.$t("ModuleDevice.buttons");
    },
    dataList() {
      return this.$t("ModuleDevice.Device");
    },
  },
  created() {
    this.getList();
  },
  methods: {
    formatTime,
    toView(row) {
      this.id = row.id;
      this.deviceBorrowViewVisible = true;
    },
    shouldHighlightRow(row, index) {
      // 根据条件判断是否应该标红行
      const now = new Date();
      const returnDate = new Date(row.returnDate);
      if (row.statusId === 50 && now > returnDate) {
        return "highlight-row";
      } else {
        return "";
      }
    },
    // 归还处理
    returnDispose(data) {
      this.id = data.id;
      this.deviceBorrowSendBackVisible = true;
    },
    // 借出确认
    lendAffirm(data) {
      this.id = data.id;
      this.lendAffirmVisible = true;
    },
    changePage(val) {
      this.pagination.pageIndex = val;
      this.getList();
    },
    sizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.getList();
    },
    cancelForm() {
      this.$refs.queryForm.resetFields();
      this.pagination.pageIndex = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      if (this.queryForm.createTimeArray.length > 0) {
        this.queryForm.startCreateTime = formatTimeMin(
          this.queryForm.createTimeArray[0]
        );
        this.queryForm.endCreateTime = formatTimeMax(
          this.queryForm.createTimeArray[1]
        );
      } else {
        this.queryForm.startCreateTime = null;
        this.queryForm.endCreateTime = null;
      }
      // this.queryForm.departId =  sessionStorage.getItem("currentDepartmentId");
      getList(this.queryForm, this.pagination)
        .then((res) => {
          this.list = res.data.dataList ? res.data.dataList : [];
          this.pagination.total = res.data.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    onNew() {
      this.dialogFormAddVisible = true;
    },
    handleRepeal(rowData) {
      this.$confirm(
        this.$t("ModuleDevice.Message.ConfirmNullify"),
        this.$t("ModuleDevice.buttons.Tips"),
        {
          confirmButtonText: this.$t("ModuleDevice.buttons.Confirm"),
          cancelButtonText: this.$t("ModuleDevice.buttons.Cancel"),
          type: "warning",
          center: true,
        }
      )
        .then(() => {
          updateStatus({
            id: rowData.id,
            statusId: 70,
            statusName: this.$t("ModuleDevice.Device.Invalid"),
          })
            .then((res) => {
              this.getList();
            })
            .catch((res) => {});
        })
        .catch(() => {
          // 用户点击取消按钮或点击遮罩层后的回调函数
          // 可以不做任何操作
        });
    },
    handleEdit(rowData) {
      this.editState = true;
      this.id = rowData.id;
      this.dialogFormEditVisible = true;
    },
    onQuery() {
      this.pagination.pageIndex = 1;
      this.getList();
    },
  },
};
</script>
<style lang="scss">
.el-table .highlight-row {
  background-color: lightpink;
}
</style>
